import {takeEvery,call,put} from 'redux-saga/effects'
import api from '../api'
import {getShopcartListAC} from '../store/actions/shopcartAction'
//完成购物车列表功能,完成异步操作
function * getShopcartListAsyncGen(){
    //在call的参数中内容是api方法名
    const result=yield call(api.shopcart.getShopcartApi)
    //在生成器调用reducer来完成数据保存
    yield put(getShopcartListAC(result.data.data))
}

function * changeNumAsyncGen({_id,n}){

  const result=yield call(api.shopcart.changeNumApi,{_id,n})
  if(result.data.code){
    yield put({type:'getShopcartListAsync'})
  }
}

function * checkProductsAsyncGen({_id}){
  console.log('---------checkProductsAsyncGen异步方法-----------');
  const result=yield call(api.shopcart.checkProductsApi,_id)
  if(result.data.code){
    yield put({type:'getShopcartListAsync'})
  }
}

function * index(){
    /*
      takeEvery的参数说明
      参数1：action type的值
    */
    yield takeEvery("getShopcartListAsync",getShopcartListAsyncGen)
    yield takeEvery("changeNumAsync",changeNumAsyncGen)
    yield takeEvery("checkProductsAsync",checkProductsAsyncGen)
}
export default index